package com.go.where.manager.action.system;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.freemarker.CustomFreemarkerMethod;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.service.system.MenuBuildService;
import com.go.where.manager.service.system.MenuService;
import com.go.where.manager.util.CommonUtil;
import com.google.gson.Gson;

/**
 * @author hzbc-01
 * 
 */
public class MenuAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(MenuAction.class);
	@Resource(name = "menuService")
	private MenuService menuService;
	@Resource(name = "menuBuildService")
	private MenuBuildService menuBuildService;
	private Menu menu;
	private String menuId;
	private String menuLevel;
	private String flag;
	private List<Menu> menuList;
	private Menu menuQuery;
	private String ts;

	public Pagination<?> page;

	Gson gson = CommonUtil.gson();

	// 添加
	public String add() {
		return INPUT;
	}

	/**
	 * 刷新菜单缓存
	 * 
	 * @return
	 */
	public String refresh() {
		menuBuildService.builMenu();
		return NONE;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			menu.setMenuUrl(CustomFreemarkerMethod.dispose(menu.getMenuUrl()));
			menuService.save(menu);
			redirectionUrl = "menu!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("数据库保存信息异常！");
			return this.add();
		}

	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (menuQuery == null) {
			menuQuery = new Menu();
			menuQuery.setPageSize(Constants.INIT_PAGESIZE);
			menuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = menuService.queryPage(menuQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}

		return LIST;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		menu = menuService.queryById(menuId);
		return INPUT;
	}

	public String edit() throws Exception {
		try {
			menu.setMenuUrl(CustomFreemarkerMethod.dispose(menu.getMenuUrl()));
			menuService.update(menu);
			redirectionUrl = "menu!list.htm";
			return "success";
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！");
			menuId = menu.getMenuId();
			return this.toEdit();
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		try {
			menuService.delete(menuId);
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常！");
			return LIST;
		}
		redirectionUrl = "menu!list.htm";
		return SUCCESS;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTs() {
		return ts;
	}

	public void setTs(String ts) {
		this.ts = ts;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getMenuLevel() {
		return menuLevel;
	}

	public void setMenuLevel(String menuLevel) {
		this.menuLevel = menuLevel;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public Menu getMenuQuery() {
		return menuQuery;
	}

	public void setMenuQuery(Menu menuQuery) {
		this.menuQuery = menuQuery;
	}

}
